package com.onehash.onebilling.entity;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/*
 * Author : Xia Xi
 * 
 */


public class TransactionRecord implements Serializable {

		private HashMap <String, ArrayList <Transaction>> txns;
		private Observer transactionObserver; //for display use
		private static TransactionRecord transactionRecordInstance;
		
		public static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
		public static String delimiter = ",";
	    public static final String TXNS_OBJ_FILENAME="data\\transaction.csv";
	    
		private TransactionRecord () throws FileNotFoundException, IOException, ParseException{
			//load from file
			txns = new HashMap <String, ArrayList <Transaction>>();
			 
			readTxnsObjects(TXNS_OBJ_FILENAME);
		}
		
		public HashMap<String, ArrayList<Transaction>> getTxns() {
			return txns;
		}


		public static TransactionRecord getInstance() throws FileNotFoundException, IOException, ParseException{
	        if(transactionRecordInstance==null){
	        	transactionRecordInstance=new TransactionRecord();
	        }
	        return transactionRecordInstance;
	    }
		
		
		
		/* Adds an observer UI class */
	    public void addObserver(Observer UI){
	    	transactionObserver = UI;
	    }
	    
	    
		public void addTransaction (String customerAcc, Date dateTime,String planType, String callType, int duration, String numberCalled){
			Transaction newTxn = new Transaction(customerAcc,dateTime, planType, callType,  duration,numberCalled);
			String key = customerAcc +delimiter+sdf.format(dateTime).substring(0, 6) +delimiter+planType+delimiter+callType; //use acc+month+planType+ callType as key
			ArrayList <Transaction> txnArrList = txns.get(key);
			
			if (txnArrList == null || txnArrList.size()==0) {
				txnArrList = new ArrayList <Transaction> ();
				txnArrList.add(newTxn);
				txns.put (key, txnArrList);
			}else {
				txnArrList.add(newTxn);
			}
			
		}

		public ArrayList <Transaction> getTxnArrListByCusomerAccMonthPlanTypeCallType(String customerAcc, String month,String planType, String callType){
			String key = customerAcc +delimiter+ month+delimiter+planType+delimiter+callType; //use acc+month+planType+ callType as key
			ArrayList <Transaction> txnArrList = txns.get(key);
			return txnArrList;			
		}
		
		public int getTxnsTotalDurationByCusomerAccMonthPlanTypeCallType(String customerAcc, String month,String planType, String callType){
			
			ArrayList <Transaction> txnArrList = getTxnArrListByCusomerAccMonthPlanTypeCallType(customerAcc, month, planType, callType);
			int duration = 0;
			for (Transaction txn : txnArrList) {
				duration += txn.getDuration();
			}
			
			return duration;			
		}
		


	    public void readTxnsObjects(String filename) throws FileNotFoundException, IOException, ParseException{
	       
	        FileInputStream fi = new FileInputStream(filename);
	   	      
	   		DataInputStream dis = new DataInputStream(fi);
	   		BufferedReader br = new BufferedReader(new InputStreamReader(dis));
	   	
	   		String strLine = ""; 
	   		while ((strLine = br.readLine()) != null)   {
	   			String [] strArr = strLine.split(delimiter);
	   			Date dateTime = (Date)sdf.parse(strArr[1]);
	   			addTransaction(strArr[0], dateTime, strArr[2], strArr[3], Integer.parseInt(strArr[4]), strArr[5]);
	   		}			
	   		    	
	    }
	    

		public static void main(String[] args) throws Exception { //for testing
			TransactionRecord tr = TransactionRecord.getInstance();
			System.out.println(tr.getTxnArrListByCusomerAccMonthPlanTypeCallType("CA0001", "201102", "Digital", "IDD").size());
			System.out.println(tr.getTxnsTotalDurationByCusomerAccMonthPlanTypeCallType("CA0001", "201102", "Digital", "IDD"));
		}
		
		/*
		public void show(){
			System.out.println ("************ Txn List **************");
			
		}
	*/

}
